package org.maven.dependencybrowser;
/**********************************************************************
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Contributors:

**********************************************************************/
import junit.framework.TestCase;
import org.apache.maven.model.Dependency;
import org.apache.maven.model.DependencyManagement;
import org.apache.maven.model.Model;
import org.apache.maven.model.Parent;
import org.maven.dependencybrowser.implementations.Repository;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;


public abstract class RepositoryTestFixture extends TestCase
{

    protected void addParent(Model model, String groupId, String artifactId, String version)
    {
        Parent parent = new Parent();
        parent.setGroupId(groupId);
        parent.setArtifactId(artifactId);
        parent.setVersion(version);
        model.setParent(parent);
    }

    protected void addDependency(Model model, String groupId, String artifactId, String version, String scope)
    {
        Dependency dependency = new Dependency();
        dependency.setGroupId(groupId);
        dependency.setArtifactId(artifactId);
        dependency.setVersion(version);
        dependency.setScope(scope);
        model.addDependency(dependency);
    }

    protected void addDependencyManagement(Model model, String groupId, String artifactId, String version, String scope)
    {
        DependencyManagement dependencyManagement = model.getDependencyManagement();
        if (dependencyManagement == null)
        {
            dependencyManagement = new DependencyManagement();
            model.setDependencyManagement(dependencyManagement);
        }
        Dependency dependency = new Dependency();
        dependency.setGroupId(groupId);
        dependency.setArtifactId(artifactId);
        dependency.setVersion(version);
        dependency.setScope(scope);
        dependencyManagement.addDependency(dependency);
    }

    protected void addProperty(Model model, String key, String value)
    {
        model.addProperty(key, value);
    }

    protected Dependency getDependency(Model model, String groupId, String artifactId)
    {
        for (Iterator iter = model.getDependencies().iterator(); iter.hasNext();)
        {
            Dependency dependency = (Dependency) iter.next();
            if (dependency.getGroupId().equals(groupId) && dependency.getArtifactId().equals(artifactId))
            {
                return dependency;
            }
        }
        return null;
    }

    protected class TestRepository implements Repository
    {

        private Map models;

        public TestRepository()
        {
            models = new HashMap();
        }

        public boolean canCreateSnapshot()
        {
            return false;
        }

        public Model resolveFile(String groupId, String artifactId, String version)
        {
            String key = groupId + ":" + artifactId + ":" + version;
            return (Model) models.get(key);
        }

        public Model createModel(String groupId, String artifactId, String version, String key)
        {
            Model model = new Model();
            model.setGroupId(groupId);
            model.setArtifactId(artifactId);
            model.setVersion(version);
            models.put(key, model);
            return model;
        }

        public Model createModel(String groupId, String artifactId, String version)
        {
            String key = groupId + ":" + artifactId + ":" + version;
            return createModel(groupId, artifactId, version, key);
        }
    }
}
